﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Kinect;

namespace KinectedMusic.Kinect
{
    public class ColourPerson
    {
        private DepthImageFrame depth;
        private Skeleton skeleton;
        private ColorImageFormat colorImageFormat;

        public Dictionary<JointType, ColourJoint> Points { get; set; }
        public ColourBox ColourBox { get; set; }

        public ColourPerson(DepthImageFrame depth, Skeleton skeleton, ColorImageFormat colorImageFormat)
        {
            this.depth = depth;
            this.skeleton = skeleton;
            this.colorImageFormat = colorImageFormat;

            Points = new Dictionary<JointType, ColourJoint>();
            AddToDictionary(skeleton.Joints[JointType.ElbowLeft]);
            AddToDictionary(skeleton.Joints[JointType.ElbowRight]);
            AddToDictionary(skeleton.Joints[JointType.HandLeft]);
            AddToDictionary(skeleton.Joints[JointType.HandRight]);
            AddToDictionary(skeleton.Joints[JointType.Head]);
            AddToDictionary(skeleton.Joints[JointType.HipCenter]);
            AddToDictionary(skeleton.Joints[JointType.HipLeft]);
            AddToDictionary(skeleton.Joints[JointType.HipRight]);
            AddToDictionary(skeleton.Joints[JointType.ShoulderCenter]);
            AddToDictionary(skeleton.Joints[JointType.ShoulderLeft]);
            AddToDictionary(skeleton.Joints[JointType.ShoulderRight]);
            AddToDictionary(skeleton.Joints[JointType.Spine]);
            AddToDictionary(skeleton.Joints[JointType.WristLeft]);
            AddToDictionary(skeleton.Joints[JointType.WristRight]);

            ColourBox = new ColourBox(Points[JointType.HipCenter], Points[JointType.ShoulderCenter]);
        }


        private void AddToDictionary(Joint joint)
        {

            Points.Add(joint.JointType, CreateColourHand(joint));
        }


        private ColourJoint CreateColourHand(Joint joint)
        {
            DepthImagePoint depthImagePoint = depth.MapFromSkeletonPoint(joint.Position);
            ColorImagePoint colorImagePoint = depth.MapToColorImagePoint(depthImagePoint.X, depthImagePoint.Y, colorImageFormat);
            //float distanceFromBody = skeleton.Joints[JointType.ShoulderCenter].Position.Z - joint.Position.Z;
            return new ColourJoint
            {
                X = colorImagePoint.X,
                Y = colorImagePoint.Y,
                Z = joint.Position.Z
            };
        }

        //private float NormaliseDistanceFromBody(float distanceFromBody)
        //{
        //    distanceFromBody /= .4f;
        //    if (distanceFromBody > 1.0f)
        //    {
        //        distanceFromBody = 1.0f;
        //    }
        //    else if (distanceFromBody < 0f)
        //    {
        //        distanceFromBody = 0f;
        //    }
        //    return distanceFromBody;
        //}

    }
}
